// import Vue from 'vue'
// import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// // 导入 welcome
// import Welcome from '../components/Welcome.vue'
// // 导入 user
// import Users from '../components/user/Users.vue'
// // 导入权限 页面
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
// // 导入  cate 页面
// import Cate from '../components/goods/Cate.vue'
// // 导入 params 页面
// import Params from '../components/goods/Params.vue'
// // 导入 List 页面
// import Goods from '../components/goods/List.vue'
// // 导入 添加商品
// import Goodsadd from '../components/goods/Add.vue'
// // 导入  订单
// import Oruder from '../components/order/Order.vue'
// // 导入 数据统计
// import Reports from '../components/reports/Reports.vue'
// =---------------------------------------------------------------------

//  懒加载
import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')

const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')

const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')

const Users = () => import(/* webpackChunkName:"user" */ '../components/user/Users.vue')

const Rights = () => import(/* webpackChunkName:"power" */ '../components/power/Rights.vue')

const Roles = () => import(/* webpackChunkName:"power" */ '../components/power/Roles.vue')

const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')

const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')

const Goods = () => import(/* webpackChunkName:"goods" */ '../components/goods/List.vue')

const Goodsadd = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')

const Oruder = () => import(/* webpackChunkName:"order" */ '../components/order/Order.vue')

const Reports = () => import(/* webpackChunkName:"report" */ '../components/reports/Reports.vue')

// ----------------------------------------------------------------------------
Vue.use(VueRouter)

const routes = [
  // 默认登录重定项
  { path: '/', redirect: '/login' },
  //   注册登录组件路由
  { path: '/login', component: Login },
  //   注册主页组件路由
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Goodsadd },
      { path: '/orders', component: Oruder },
      { path: '/reports', component: Reports }

    ]
  }
]

// 创建路由管理器
const router = new VueRouter({
  routes
})

//  路由守卫
router.beforeEach((to, from, next) => {
  //   to 是访问的路径
  // from 从哪个路径跳转过来
  //  next () 表示方行   next（‘./login’）可以直接跟地址
  if (to.path === '/login') return next()
  // 获取 token
  const tokenstr = window.sessionStorage.getItem('token')
  // console.log(tokenstr);
  // 没有就跳转
  if (!tokenstr) return next('login')
  next()
})

// 暴露出去
export default router
